//
//  RightTabBarController.m
//  觅影
//
//  Created by  陶虹 on 16/4/6.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "RightTabBarController.h"
#import "MovieLibraryViewController.h"
#import "MovieFriendsViewController.h"
#import "MicroMovieViewController.h"
#import "TopicsViewController.h"
#import "RootViewController.h"
#import "BaseViewController.h"
@interface RightTabBarController ()<PushDelegate, UITabBarControllerDelegate>
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIBarButtonItem *changeFrameButton;
@end

@implementation RightTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.changeFrameButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"root.png"] style:UIBarButtonItemStylePlain target:self action:@selector(changeFrameButtonAction:)];
    // 影库
    MovieLibraryViewController *movieLibraryVc = [[MovieLibraryViewController alloc] init];
    UINavigationController *movieLibraryNav = [[UINavigationController alloc] initWithRootViewController:movieLibraryVc];
    movieLibraryVc.navigationItem.leftBarButtonItem = self.changeFrameButton;
    [movieLibraryVc.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    movieLibraryNav.tabBarItem.title = @"影库";
    movieLibraryNav.tabBarItem.tag = 0;
    movieLibraryNav.tabBarItem.image = [UIImage imageNamed:@"library"];
    
    // 影友
    MovieFriendsViewController *movieFirendsVc = [[MovieFriendsViewController alloc] init];
    UINavigationController *movieFriendsNav = [[UINavigationController alloc] initWithRootViewController:movieFirendsVc];
    movieFirendsVc.navigationItem.leftBarButtonItem = self.changeFrameButton;
    [movieFirendsVc.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    movieFriendsNav.tabBarItem.title = @"影友";
    movieFriendsNav.tabBarItem.tag = 1;
    movieFriendsNav.tabBarItem.image = [UIImage imageNamed:@"friend"];
    
    // 微电影
    MicroMovieViewController *microFilmsVc = [[MicroMovieViewController alloc] init];
    UINavigationController *microFilmsNav = [[UINavigationController alloc] initWithRootViewController:microFilmsVc];
    microFilmsVc.navigationItem.leftBarButtonItem = self.changeFrameButton;
    [microFilmsVc.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    microFilmsNav.tabBarItem.title = @"微影";
    microFilmsNav.tabBarItem.tag = 2;
    microFilmsNav.tabBarItem.image = [UIImage imageNamed:@"micro"];
    
    // 话题
    TopicsViewController *topicsVc = [[TopicsViewController alloc] init];
    UINavigationController *topicsNav = [[UINavigationController alloc] initWithRootViewController:topicsVc];
    topicsVc.navigationItem.leftBarButtonItem = self.changeFrameButton;
    [topicsVc.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    topicsNav.tabBarItem.title = @"话题";
    topicsNav.tabBarItem.tag = 3;
    topicsNav.tabBarItem.image = [UIImage imageNamed:@"topic"];
    
   
    self.viewControllers = @[movieLibraryNav, movieFriendsNav, microFilmsNav, topicsNav];
    [self.tabBar setTintColor:[UIColor blackColor]];
    // 轻拍手势
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:self.tap];
    self.tap.enabled = NO;
    
    // 平移手势
    self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:_pan];
    
    // 获取当前nav
    _index = 0;
    self.currentNav = [self.viewControllers objectAtIndex:_index];

}

#pragma mark --- 打开抽屉 ---
- (void)openDrawer{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(KWIDTH - 64, 0, KWIDTH, KHEIGHT);
    }completion:^(BOOL finished) {
        self.currentNav.view.userInteractionEnabled = NO;
        self.tap.enabled = YES;
    }];
}

#pragma mark --- 关闭抽屉 ---
- (void)closeDrawer{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
    } completion:^(BOOL finished) {
        self.currentNav.view.userInteractionEnabled = YES;
        self.tap.enabled = NO;
    }];
}

#pragma mark --- 执行代理方法 ---
- (void)pushToVc:(BaseViewController *)Vc{
    Vc.hidesBottomBarWhenPushed = YES;
    [self.currentNav pushViewController:Vc animated:YES];
    [self closeDrawer];
}

#pragma mark --- 轻拍手势 ---
- (void)tapAction:(UIGestureRecognizer *)tap{
    [self closeDrawer];
}

#pragma mark --- 平移手势 ---
- (void)panAction:(UIPanGestureRecognizer *)pan{
   
    CGPoint point = [pan translationInView:self.view];
    self.view.transform = CGAffineTransformTranslate(self.view.transform, point.x, 0);
    [pan setTranslation:CGPointZero inView:self.view];
    
    // 获取手指位置
    CGPoint thumbPoint = [pan locationInView:self.view];
    
    // 判断右边缘不让其拖动
    CGRect newFrame = self.view.frame;
    if (self.view.frame.origin.x < 0) {
        newFrame.origin.x = 0;
    }
    if (self.view.frame.origin.x >= KWIDTH - 64) {
        newFrame.origin.x = KWIDTH - 64;
    }
    if (thumbPoint.x > 64) {
        newFrame.origin.x = 0;
    }
    self.view.frame = newFrame;
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.view.frame.origin.x < (KWIDTH - 64) * 0.5) {
            [self closeDrawer];
        }
        else{
            [self openDrawer];
        }
    }

}

- (void)changeFrameButtonAction:(UIButton *)button{    
    if (self.view.frame.origin.x == 0) {
        [self openDrawer];
    }else{
        [self closeDrawer];
    }
}

#pragma mark --- 点击barBarItem的方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    self.index = item.tag;
    self.currentNav = [self.viewControllers objectAtIndex:_index];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
